package com.happyscan138.makefun;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.happyscan138.makefun.Fragment.myfriend_fans;
import com.happyscan138.makefun.Fragment.myfriend_notice;
import com.happyscan138.makefun.adapter.MyFragmentPagerAdapter;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/12/5.
 */

public class MyFriend extends AppCompatActivity implements View.OnClickListener {
    Resources resources;
    private TextView textViewfans;
    private TextView textViewnotice;
    private ArrayList<Fragment> fragmentsList;
    private ViewPager mPagerMyFriend;
    private ImageView imagetab;//下划线
    private int position_one;//初始位移
    private int pWidth;//宽度
    private int offset ;// 图片偏移量
    public final static int num = 2;
    private int currIndex = 0;
    private ImageButton back;
    Fragment notice;
    Fragment fans;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_friend);
        initView();
        InitViewPager();
        InitWidth();
        TranslateAnimation animation = new TranslateAnimation(0, offset, 0, 0);
        animation.setFillAfter(true);
        animation.setDuration(300);
        imagetab.startAnimation(animation);
    }

    public void initView(){
        imagetab=(ImageView)findViewById(R.id.my_friend_tab);
        textViewnotice=(TextView)findViewById(R.id.text_notice);
        textViewfans=(TextView)findViewById(R.id.text_fans);
        back=(ImageButton)findViewById(R.id.my_friend_back);
        back.setOnClickListener(this);

        textViewfans.setOnClickListener(this);
        textViewnotice.setOnClickListener(this);
    }
    private void InitViewPager( ) {
        mPagerMyFriend = (ViewPager)findViewById(R.id.my_friend);
        fragmentsList = new ArrayList<Fragment>();
        notice = new myfriend_notice();
        fans = new myfriend_fans();
        fragmentsList.add(notice);
        fragmentsList.add(fans);

        mPagerMyFriend.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mPagerMyFriend.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Animation animation = null;
                switch (position) {
                    case 0:
                        if (currIndex == 1) {
                            animation = new TranslateAnimation(position_one, offset, 0, 0);
                        }

                        break;
                    case 1:
                        if (currIndex == 0) {
                            animation = new TranslateAnimation(offset, position_one, 0, 0);
                        }

                        break;
                }
                currIndex = position;
                animation.setFillAfter(true);
                animation.setDuration(300);
                imagetab.startAnimation(animation);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
        });
        mPagerMyFriend.setCurrentItem(0);



    }
    private void InitWidth( ) {
        imagetab = (ImageView) findViewById(R.id.my_friend_tab);
        BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(R.mipmap.up_tab);
        Bitmap bmp = bd.getBitmap();

        pWidth = bmp.getWidth();
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        offset = (int) ((screenW / num -pWidth)) / 2;//偏移量
        int avg = (int) (screenW / num);
        position_one = avg+offset;//起始位置

    }


    @Override
    public void onClick(View view) {
         switch (view.getId()){
             case R.id.text_notice:
                 mPagerMyFriend.setCurrentItem(0);
                 break;
             case R.id.text_fans:
                 mPagerMyFriend.setCurrentItem(1);
                 break;
             case R.id.my_friend_back:
                 finish();
                 break;
         }

    }
}
